﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using YogaStudio.Data;
using YogaStudio.Data.Domain;
using YogaStudio.Service.BLL;

namespace YogaStudio.WebUI.Controllers
{
   // [Authorize]
    public class TranscriptsController : Controller
    {
        private YogaStudioEntities db = new YogaStudioEntities();
        CustomerService customerService = new CustomerService();
        UserManagementService userManagement = new UserManagementService();
        User user;
        // GET: Transcripts
        public ActionResult Index()
        {
            //TB
            user = userManagement.GetUser(User.Identity.Name);
          //  Customer customer = customerService.GetCustomerByUserId(user.UserId);
            if (user == null)
            {
                return HttpNotFound();
            }
          
            var transcripts = db.Transcripts.Where(e=>e.CustomerId==user.UserId).Include(t => t.Customer).Include(t => t.Section);
            return View(transcripts.ToList());
        }

        // GET: Transcripts/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Transcript transcript = db.Transcripts.Find(id);
            if (transcript == null)
            {
                return HttpNotFound();
            }
            return View(transcript);
        }

        // GET: Transcripts/Create
        public ActionResult Create()
        {
            ViewBag.CustomerId = new SelectList(db.Customers, "Id", "FullName");
            ViewBag.SectionId = new SelectList(db.Sections, "Id", "Id");
            return View();
        }

        // POST: Transcripts/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "TransId,SectionId,CustomerId,Grade,CourseDate")] Transcript transcript)
        {
            if (ModelState.IsValid)
            {
                db.Transcripts.Add(transcript);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.CustomerId = new SelectList(db.Customers, "Id", "FullName", transcript.CustomerId);
            ViewBag.SectionId = new SelectList(db.Sections, "Id", "Id", transcript.SectionId);
            return View(transcript);
        }
        public ActionResult Unenroll(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Transcript transcript = db.Transcripts.Find(id);
            Section sec = db.Sections.Find(transcript.SectionId);

            db.CoursePaymentDetails.Remove(transcript.CoursePaymentDetail);
            db.Transcripts.Remove(transcript);
            Waitinglist wait = db.WaitingLists.Where(e => e.SectionId == sec.Id).OrderByDescending(d => d.Waitingdate).FirstOrDefault();
            if (wait == null)
            {
                sec.Capacity++;
                db.Entry(sec).State = EntityState.Modified;
            }
            else
            {
                Transcript t = new Transcript()
                {
                    CustomerId = wait.CustomerId,
                    SectionId = wait.SectionId,
                    Grade = 0,
                    CourseDate =wait.Section.Schedule.FromDate,
                };
                db.Transcripts.Add(t);
                
                db.WaitingLists.Remove(wait);

            }
            db.SaveChanges();

          
            return View("Unenroll");
        }
        // GET: Transcripts/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Transcript transcript = db.Transcripts.Find(id);
            if (transcript == null)
            {
                return HttpNotFound();
            }
            ViewBag.CustomerId = new SelectList(db.Customers, "Id", "FullName", transcript.CustomerId);
            ViewBag.SectionId = new SelectList(db.Sections, "Id", "Id", transcript.SectionId);
            return View(transcript);
        }

        // POST: Transcripts/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "TransId,SectionId,CustomerId,Grade,CourseDate")] Transcript transcript)
        {
            if (ModelState.IsValid)
            {
                db.Entry(transcript).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.CustomerId = new SelectList(db.Customers, "Id", "FullName", transcript.CustomerId);
            ViewBag.SectionId = new SelectList(db.Sections, "Id", "Id", transcript.SectionId);
            return View(transcript);
        }

        // GET: Transcripts/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Transcript transcript = db.Transcripts.Find(id);
            if (transcript == null)
            {
                return HttpNotFound();
            }
            return View(transcript);
        }

        // POST: Transcripts/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            Transcript transcript = db.Transcripts.Find(id);
            db.Transcripts.Remove(transcript);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
